package com.tenc.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Alan
 * @date 2021/7/20
 * @description 响应实体
 * @ClassName ApiRest
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Accessors(chain = true)
public class ApiRest {

    @ApiModelProperty("响应状态码")
    private Integer code;
    @ApiModelProperty("响应消息")
    private String message;
    @ApiModelProperty("响应数据")
    private Map<String,Object> data;
    @ApiModelProperty("成功：true 失败：false")
    private Boolean success;

    public static ApiRest ok(){
       return ApiRest.builder()
                .code(ApiEnum.SUCCESS.getCode())
                .message(ApiEnum.SUCCESS.getMessage())
                .success(true).build();
    }

    public static ApiRest ok(String message){
        return ApiRest.builder()
                .code(ApiEnum.SUCCESS.getCode())
                .message(message)
                .success(true).build();
    }

    public static ApiRest result(Map<String,Object> data){

        return ApiRest.builder()
                .code(ApiEnum.SUCCESS.getCode())
                .message(ApiEnum.SUCCESS.getMessage())
                .data(data)
                .success(true).build();

    }
    public static ApiRest result(String key,Object data){
        Map<String,Object> res = new HashMap<>();
        res.put(key,data);
        return ApiRest.builder()
                .code(ApiEnum.SUCCESS.getCode())
                .message(ApiEnum.SUCCESS.getMessage())
                .data(res)
                .success(true).build();

    }


    public static ApiRest fail(){
        return  ApiRest.builder()
                .code(ApiEnum.FAIL.getCode())
                .message(ApiEnum.FAIL.getMessage())
                .success(false)
                .data(null)
                .build();
    }
    public static ApiRest fail(String msg){
        return  ApiRest.builder()
                .code(ApiEnum.FAIL.getCode())
                .message(msg)
                .success(false)
                .data(null)
                .build();
    }
}
